﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApp
{
    public partial class ViewOrdersforUser : System.Web.UI.Page
    {
        string strConnect;
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        SqlDataReader dr;
        protected void Page_Load(object sender, EventArgs e)
        {
            strConnect = "Data Source = (local); Database =ProjectSem3; Trusted_Connection = true";
            con = new SqlConnection(strConnect);
            ds = new DataSet();
            if (!Page.IsPostBack)
            {
                try
                {
                    Bindordres();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }
        protected int GetCusID()
    {
        try
        {


            if (con.State == ConnectionState.Closed)
                con.Open();
            string q = "select cusID from customer where username = '" + Session["userid"].ToString() + "'";
            cmd = new SqlCommand(q, con);
            cmd.CommandType = CommandType.Text;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "customer");
            
            
            return Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString()); 

        }
        catch (SqlException ex)
        {
            //throw new Exception(ex.Message);
            return 0;
        }
        finally
        {
            con.Close();
        }
    }

        protected void Bindordres()
        {
            try
            {

                
                if (con.State == ConnectionState.Closed)
                    con.Open();
                string q = "select * from orders where cusID = " + GetCusID();
                cmd = new SqlCommand(q, con);
                cmd.CommandType = CommandType.Text;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "orders");
                GridView1.DataSource = ds.Tables["orders"];
                GridView1.DataBind();
               

            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            try
            {


                if (con.State == ConnectionState.Closed)
                    con.Open();
                string q = "select * from orders where ordersid = " + txtSearch.Text;
                cmd = new SqlCommand(q, con);
                cmd.CommandType = CommandType.Text;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "orders");
                GridView1.DataSource = ds.Tables["orders"];
                GridView1.DataBind();


            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
      
    }
}